<?php

namespace app\rental\controller;

use app\rental\model\RentalRankModel;
use cmf\controller\AdminBaseController;
use think\Db;

/**
 * Class AdminTagController 汽车级别管理控制器
 * @package app\rental\controller
 */
class AdminRankController extends AdminBaseController
{
    /**
     * 汽车级别管理
     * @adminMenu(
     *     'name'   => '汽车级别',
     *     'parent' => 'rental/AdminIndex/default',
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 4,
     *     'icon'   => '',
     *     'remark' => '汽车级别',
     *     'param'  => ''
     * )
     */
    public function index()
    {
        $rentalRankModel = new RentalRankModel();
        $ranks           = $rentalRankModel->paginate();

        $this->assign("arrStatus", $rentalRankModel::$STATUS);
        $this->assign("ranks", $ranks);
        $this->assign('page', $ranks->render());
        return $this->fetch();
    }

    /**
     * 添加汽车级别
     * @adminMenu(
     *     'name'   => '添加汽车级别',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 0,
     *     'icon'   => '',
     *     'remark' => '添加汽车级别',
     *     'param'  => ''
     * )
     */
    public function add()
    {
        $rentalRankModel = new RentalRankModel();
        $this->assign("arrStatus", $rentalRankModel::$STATUS);
        return $this->fetch();
    }

    /**
     * 添加汽车级别提交
     * @adminMenu(
     *     'name'   => '添加汽车级别提交',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 0,
     *     'icon'   => '',
     *     'remark' => '添加汽车级别提交',
     *     'param'  => ''
     * )
     */
    public function addPost()
    {

        $arrData = $this->request->param();
        $result = $this->validate($arrData, 'RentalRank');
        $rentalRankModel = new RentalRankModel();
        $rentalRankModel->isUpdate(false)->allowField(true)->save($arrData);

        $this->success(lang("SAVE_SUCCESS"),url('AdminRank/index'));

    }
    /**
     * 编辑汽车级别
     * @adminMenu(
     *     'name'   => '编辑汽车级别',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 0,
     *     'icon'   => '',
     *     'remark' => '编辑汽车级别',
     *     'param'  => ''
     * )
     */
    public function edit()
    {
        $id = $this->request->param('id', 0, 'intval');
        if ($id > 0) {
            $rentalRankModel = new RentalRankModel();
            $rank = $rentalRankModel->get($id)->toArray();
            $this->assign("arrStatus", $rentalRankModel::$STATUS);
            $this->assign("rank", $rank);
            return $this->fetch();
        } else {
            $this->error('操作错误!');
        }

    }
    public function editPost()
    {
        $intId     = $this->request->param("id");
        $intStatus = $this->request->param("status");
        $intRank = $this->request->param("rank");
        $intStatus = $intStatus ? 1 : 0;
        if (empty($intId)) {
            $this->error('操作错误');
        }
        $rentalRankModel = new RentalRankModel();
        $rentalRankModel->isUpdate(true)->save(["status" => $intStatus,"rank" => $intRank], ["id" => $intId]);
        $this->success('保存成功',url('AdminRank/index'));

    }
    /**
     * 更新汽车级别状态
     * @adminMenu(
     *     'name'   => '更新标签状态',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 0,
     *     'icon'   => '',
     *     'remark' => '更新标签状态',
     *     'param'  => ''
     * )
     */
    public function upStatus()
    {
        $intId     = $this->request->param("id");
        $intStatus = $this->request->param("status");
        $intStatus = $intStatus ? 1 : 0;
        if (empty($intId)) {
            $this->error(lang("NO_ID"));
        }

        $rentalRankModel = new RentalRankModel();
        $rentalRankModel->isUpdate(true)->save(["status" => $intStatus], ["id" => $intId]);

        $this->success(lang("SAVE_SUCCESS"));

    }

    /**
     * 删除汽车级别
     * @adminMenu(
     *     'name'   => '删除汽车级别',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 0,
     *     'icon'   => '',
     *     'remark' => '删除汽车级别',
     *     'param'  => ''
     * )
     */
    public function delete()
    {
        $intId = $this->request->param("id", 0, 'intval');

        if (empty($intId)) {
            $this->error(lang("NO_ID"));
        }
        $rentalRankModel = new RentalRankModel();

        $rentalRankModel->where(['id' => $intId])->delete();

        $this->success(lang("DELETE_SUCCESS"));
    }
}
